package com.atguigu.interceptor;


import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.pojo.h5.UserInfo;
import com.atguigu.spzx.model.pojo.h5.UserInfoAuthContextUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/*
 * @description: 拦截器拦截获取当前用户信息请求，将信息存入本地线程中
 **/
@Component
public class CurrentUserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("currentUser拦截器拦截");
        String userJson = request.getHeader("userJson");
        if (StringUtils.isNotEmpty(userJson)){
            UserInfo userInfo = JSON.parseObject(userJson, UserInfo.class);
            UserInfoAuthContextUtil.set(userInfo);
        }
        return true;
    }
}
